package com.easylinkin.linkappapi.lobar.util;

import java.text.DecimalFormat;

/**
 * @Author: kan yuanfeng
 * @Date: 2022/4/27 10:07
 * @Description: 百分比计算
 */
public class PercentageUtils {

  /**
   * int类型计算
   */
  public static String compute(Object part, Object total){
    DecimalFormat df1 = new DecimalFormat("0.00");
    float f = ((float) part/(float) total)*100;
    return df1.format(f);
  }

  /**
   * int类型计算(带%)
   */
  public static String computePercent(Object part, Object total){
    DecimalFormat df1 = new DecimalFormat("0.00");
    float f = ((float) part/(float) total)*100;
    return df1.format(f)+"%";
  }

  /**
   * int类型计算(带%) 保留一位小数
   */
  public static String computePercentOne(Object part, Object total){
    float p;
    float t;
    DecimalFormat df1 = new DecimalFormat("0.0");
    if (part instanceof Integer){
      p = ((Integer) part).floatValue();
    }else {
      p = (float) part;
    }
    if (total instanceof Integer){
      t = ((Integer) total).floatValue();
    }else {
      t = (float) total;
    }
    if (t==0){
      return "0.0%";
    }
    float f = p/t*100;
    return df1.format(f)+"%";
  }

  /**
   * int类型计算(带%) 保留一位小数
   */
  public static String computePercentTwo(Object part, Object total){
    float p;
    float t;
    DecimalFormat df1 = new DecimalFormat("0.00");
    if (part instanceof Integer){
      p = ((Integer) part).floatValue();
    }else {
      p = (float) part;
    }
    if (total instanceof Integer){
      t = ((Integer) total).floatValue();
    }else {
      t = (float) total;
    }
    if (t==0){
      return "0.00%";
    }
    float f = p/t*100;
    return df1.format(f)+"%";
  }
}
